package humanMirror.controller;

import humanMirror.controller.actions.Action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class Start extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected ActionFactory factory = new ActionFactory();

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String prevUrl=null;
		Action action = factory.create(getActionName(request));
		String url = action.perform(request, response);
		if (url != null) {
           /* if(prevUrl=="/quiz.jsp" && url=="/quiz.jsp"){
                int questionNumber = (Integer) request.getSession().getAttribute("questionNumber");
                request.getSession().setAttribute("questionNumber", questionNumber+1);
            }*/
			getServletContext().getRequestDispatcher(url).forward(request, response);
        }
	}

	protected String getActionName(HttpServletRequest request) {
		String path = request.getServletPath();
		return path.substring(1, path.lastIndexOf("."));
	}
}
